package com.neusoft.yuejian12.yuejian_12.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.activity.ProCourseDetailActivity;
import com.neusoft.yuejian12.yuejian_12.javabean.ProBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.PROCOURSE_PHOTO_URL;

/**
 * Created by ZTY on 2017/10/25.
 */

public class ProCourseListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List mDataList;

    private LayoutInflater mLayoutInflatera;

    public ProCourseListAdapter(Context mContext,List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflatera = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflatera.from(parent.getContext())
                .inflate(R.layout.cardview_procourse, parent, false);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ProBean entity = (ProBean) mDataList.get(position);
        if (null == entity)
            return;
        //绑定数据
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.courseName.setText(entity.getPc_name());
        viewHolder.courseTime.setText(entity.getPc_time());
        String ProcoursePic = PROCOURSE_PHOTO_URL+entity.getPc_pic();
        Picasso.with(mContext).load(ProcoursePic).into(viewHolder.procoursePic);
        if (viewHolder.procoursePic.getDrawable()==null){
            viewHolder.procoursePic.setImageResource(R.drawable.ic_add);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("pm_id",entity.getPm_id());
                bundle.putInt("pc_id",entity.getPc_id());
                bundle.putString("pc_info",entity.getPc_info());
                bundle.putString("pc_pic",entity.getPc_pic());
                bundle.putString("pc_name",entity.getPc_name());
                bundle.putString("pc_time",entity.getPc_time());
                Intent intent=new Intent(mContext,ProCourseDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);

            }
        });

    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView procoursePic;
        TextView courseName;
        TextView courseTime;

        public ViewHolder(View itemView){
            super(itemView);
            courseName = (TextView) itemView.findViewById(R.id.course_name);
            courseTime = (TextView) itemView.findViewById(R.id.course_time);
            procoursePic = (ImageView) itemView.findViewById(R.id.course_image);
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
}
